<!DOCTYPE html>
<!--
Copyright (C) 2016 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-account-entry</title>

<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<script src="../../../scripts/util.js"></script>

<link rel="import" href="gr-account-entry.html">

<script>void(0);</script>

<test-fixture id="basic">
  <template>
    <gr-account-entry></gr-account-entry>
  </template>
</test-fixture>

<script>
  suite('gr-account-entry tests', () => {
    let sandbox;
    let _nextAccountId = 0;
    const makeAccount = function(opt_status) {
      const accountId = ++_nextAccountId;
      return {
        _account_id: accountId,
        name: 'name ' + accountId,
        email: 'email ' + accountId,
        status: opt_status,
      };
    };
    let _nextAccountId2 = 0;
    const makeAccount2 = function(opt_status) {
      const accountId2 = ++_nextAccountId2;
      return {
        _account_id: accountId2,
        email: 'email ' + accountId2,
        status: opt_status,
      };
    };

    let owner;
    let existingReviewer1;
    let existingReviewer2;
    let suggestion1;
    let suggestion2;
    let suggestion3;
    let element;

    setup(() => {
      owner = makeAccount();
      existingReviewer1 = makeAccount();
      existingReviewer2 = makeAccount();
      suggestion1 = {account: makeAccount()};
      suggestion2 = {account: makeAccount()};
      suggestion3 = {
        group: {
          id: 'suggested group id',
          name: 'suggested group',
        },
      };

      element = fixture('basic');
      element.change = {
        owner,
        reviewers: {
          CC: [existingReviewer1],
          REVIEWER: [existingReviewer2],
        },
      };
      sandbox = sinon.sandbox.create();
    });

    teardown(() => {
      sandbox.restore();
    });

    suite('stubbed values for _getReviewerSuggestions', () => {
      setup(() => {
        stub('gr-rest-api-interface', {
          getChangeSuggestedReviewers() {
            const redundantSuggestion1 = {account: existingReviewer1};
            const redundantSuggestion2 = {account: existingReviewer2};
            const redundantSuggestion3 = {account: owner};
            return Promise.resolve([redundantSuggestion1, redundantSuggestion2,
              redundantSuggestion3, suggestion1, suggestion2, suggestion3]);
          },
        });
      });

      test('_makeSuggestion formats account or group accordingly', () => {
        let account = makeAccount();
        const account2 = makeAccount2();
        let suggestion = element._makeSuggestion({account});
        assert.deepEqual(suggestion, {
          name: account.name + ' <' + account.email + '>',
          value: {account},
        });

        const group = {name: 'test'};
        suggestion = element._makeSuggestion({group});
        assert.deepEqual(suggestion, {
          name: group.name + ' (group)',
          value: {group},
        });

        suggestion = element._makeSuggestion(account);
        assert.deepEqual(suggestion, {
          name: account.name + ' <' + account.email + '>',
          value: {account, count: 1},
        });

        element._config = {
          user: {
            anonymous_coward_name: 'Anonymous Coward',
          },
        };
        assert.deepEqual(element._accountOrAnon(account2), 'Anonymous');

        account = makeAccount('OOO');

        suggestion = element._makeSuggestion({account});
        assert.deepEqual(suggestion, {
          name: account.name + ' <' + account.email + '> (OOO)',
          value: {account},
        });

        suggestion = element._makeSuggestion(account);
        assert.deepEqual(suggestion, {
          name: account.name + ' <' + account.email + '> (OOO)',
          value: {account, count: 1},
        });
      });

      test('_getReviewerSuggestions excludes owner+reviewers', done => {
        element._getReviewerSuggestions().then(reviewers => {
          // Default is no filtering.
          assert.equal(reviewers.length, 6);

          // Set up filter that only accepts suggestion1.
          const accountId = suggestion1.account._account_id;
          element.filter = function(suggestion) {
            return suggestion.account &&
                suggestion.account._account_id === accountId;
          };

          element._getReviewerSuggestions().then(reviewers => {
            assert.deepEqual(reviewers, [element._makeSuggestion(suggestion1)]);
          }).then(done);
        });
      });
    });

    test('allowAnyUser', done => {
      const suggestReviewerStub =
          sandbox.stub(element.$.restAPI, 'getChangeSuggestedReviewers')
          .returns(Promise.resolve([]));
      const suggestAccountStub =
          sandbox.stub(element.$.restAPI, 'getSuggestedAccounts')
          .returns(Promise.resolve([]));

      element._getReviewerSuggestions('').then(() => {
        assert.isTrue(suggestReviewerStub.calledOnce);
        assert.isFalse(suggestAccountStub.called);
        element.allowAnyUser = true;

        element._getReviewerSuggestions('').then(() => {
          assert.isTrue(suggestReviewerStub.calledOnce);
          assert.isTrue(suggestAccountStub.calledOnce);
          done();
        });
      });
    });

    test('setText', () => {
      // Spy on query, as that is called when _updateSuggestions proceeds.
      const suggestSpy = sandbox.spy(element.$.input, 'query');
      element.setText('test text');
      flushAsynchronousOperations();

      assert.equal(element.$.input.$.input.value, 'test text');
      assert.isFalse(suggestSpy.called);
    });
  });
</script>
